package com.g2d;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.animation.Animation;

public class MySurfaceView extends SurfaceView implements Callback, Runnable {

	private SurfaceHolder sfh;
	private Thread th;
	private Canvas canvas; 
	private Paint paint; 
	private int ScreenW, ScreenH;  


	public MySurfaceView(Context context) { 
		super(context); 
		th = new Thread(this); 
		sfh = this.getHolder(); 
		sfh.addCallback(this); 

		paint = new Paint(); 
		paint.setAntiAlias(true); 
		paint.setColor(Color.RED); 
		//keep the screen lighting
		this.setKeepScreenOn(true);
	} 
	@Override

	public void startAnimation(Animation animation) { 
		super.startAnimation(animation); 
	} 
	
	public void surfaceCreated(SurfaceHolder holder) { 
		ScreenW = this.getWidth();
		ScreenH = this.getHeight(); 
		th.start(); 
	} 
	
	private void draw() { 
		try {
			canvas = sfh.lockCanvas(); 
			canvas.drawColor(Color.WHITE);//draw screen to white.
			canvas.drawText("Himi", 100, 100, paint);//draw text.
			canvas.drawText("this is an simple game framework", 100, 130, paint); 
			sfh.unlockCanvasAndPost(canvas);//commit the draw.
		} catch (Exception ex) {
			
		} finally { 
			if (canvas != null) 
				sfh.unlockCanvasAndPost(canvas); 
		} 
		
	}  
	
	public void run() { 
		while (true) { 
			draw(); 
			try { 
				Thread.sleep(100); 
			} catch (InterruptedException e) {
				e.printStackTrace(); 
			} 
		} 
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
	}
	
	public void surfaceDestroyed(SurfaceHolder holder) { 
	}

}  

